/**
 * 认证相关的验证 Schema
 */

import { Type, Static } from '@sinclair/typebox'
import { EmailSchema, PasswordSchema } from './common.schema'

// ==================== 登录请求 ====================

/**
 * 管理员登录请求
 */
export const AdminLoginSchema = Type.Object(
    {
        email: EmailSchema,
        password: PasswordSchema
    },
    {
        $id: 'AdminLogin',
        description: '管理员登录请求'
    }
)

export type AdminLoginRequest = Static<typeof AdminLoginSchema>

/**
 * 成员微信登录请求
 */
export const MemberLoginSchema = Type.Object(
    {
        wxOpenId: Type.String({
            minLength: 1,
            maxLength: 100,
            description: '微信 OpenID'
        })
    },
    {
        $id: 'MemberLogin',
        description: '成员微信登录请求'
    }
)

export type MemberLoginRequest = Static<typeof MemberLoginSchema>

// ==================== 登录响应 ====================

/**
 * 登录成功响应
 */
export const LoginResponseSchema = Type.Object(
    {
        success: Type.Literal(true),
        data: Type.Object({
            token: Type.String({ description: 'JWT Token' }),
            userId: Type.String({ description: '用户ID' }),
            userType: Type.Union([Type.Literal('admin'), Type.Literal('member')], {
                description: '用户类型'
            }),
            role: Type.Optional(Type.String({ description: '角色' })),
            organizationId: Type.Optional(Type.String({ description: '组织ID' }))
        })
    },
    {
        $id: 'LoginResponse',
        description: '登录成功响应'
    }
)

export type LoginResponse = Static<typeof LoginResponseSchema>

// ==================== Token 验证 ====================

/**
 * Token 验证请求
 */
export const VerifyTokenSchema = Type.Object(
    {
        token: Type.String({
            minLength: 1,
            description: 'JWT Token'
        })
    },
    {
        $id: 'VerifyToken',
        description: 'Token 验证请求'
    }
)

export type VerifyTokenRequest = Static<typeof VerifyTokenSchema>

/**
 * Token 验证响应
 */
export const TokenPayloadResponseSchema = Type.Object(
    {
        success: Type.Literal(true),
        data: Type.Object({
            userId: Type.String({ description: '用户ID' }),
            userType: Type.Union([Type.Literal('admin'), Type.Literal('member')], {
                description: '用户类型'
            }),
            role: Type.Optional(Type.String({ description: '角色' })),
            organizationId: Type.Optional(Type.String({ description: '组织ID' })),
            iat: Type.Optional(Type.Integer({ description: '签发时间' })),
            exp: Type.Optional(Type.Integer({ description: '过期时间' }))
        })
    },
    {
        $id: 'TokenPayloadResponse',
        description: 'Token 验证响应'
    }
)

// ==================== 刷新 Token ====================

/**
 * 刷新 Token 请求
 */
export const RefreshTokenSchema = Type.Object(
    {
        refreshToken: Type.Optional(
            Type.String({
                description: 'Refresh Token（可选，也可以从 Authorization header 获取）'
            })
        )
    },
    {
        $id: 'RefreshToken',
        description: '刷新 Token 请求'
    }
)

export type RefreshTokenRequest = Static<typeof RefreshTokenSchema>

// ==================== 修改密码 ====================

/**
 * 修改密码请求
 */
export const ChangePasswordSchema = Type.Object(
    {
        oldPassword: PasswordSchema,
        newPassword: PasswordSchema
    },
    {
        $id: 'ChangePassword',
        description: '修改密码请求'
    }
)

export type ChangePasswordRequest = Static<typeof ChangePasswordSchema>

// ==================== 重置密码 ====================

/**
 * 重置密码请求（忘记密码）
 */
export const ResetPasswordSchema = Type.Object(
    {
        email: EmailSchema,
        code: Type.String({
            minLength: 6,
            maxLength: 6,
            pattern: '^\\d{6}$',
            description: '验证码'
        }),
        newPassword: PasswordSchema
    },
    {
        $id: 'ResetPassword',
        description: '重置密码请求'
    }
)

export type ResetPasswordRequest = Static<typeof ResetPasswordSchema>

/**
 * 发送重置密码验证码请求
 */
export const SendResetCodeSchema = Type.Object(
    {
        email: EmailSchema
    },
    {
        $id: 'SendResetCode',
        description: '发送重置密码验证码'
    }
)

export type SendResetCodeRequest = Static<typeof SendResetCodeSchema>
